#include <iostream>
#include <algorithm>

using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
    ListNode *pre;
};

ListNode *createList()
{
    ListNode *preHead = new ListNode;
    ListNode *head = new ListNode;
    preHead ->next = head;
    head->next = head;
    head->pre = head;

    return preHead;
}

void insertNodeList(ListNode *preHead,int data)
{
    ListNode *head = preHead->next;

    if(! head->val) //首节点输入数据*****
    {
        head->val = data;
        return ;
    }

    ListNode *cur = new ListNode;
    cur->val = data;

    //判断新来结点的数据 放在恰当的顺序
    //三种情况  头 中 尾
    if(data < head->val)
    {
        preHead->next = cur;
        head->pre->next = cur;
        cur->pre = head->pre;
        head->pre = cur;
        cur->next = head;
    }
    else
    {
        ListNode *t = head->next;

        while(t != head->pre || head->pre == head->next)
        {
            //在中间插入
            if(data < t->next->val)
            {
                t->next->pre = cur;
                cur->next = t->next;
                cur->pre = t;
                t->next =cur;

                return ;
            }

            t =t->next;
        }

        //否则 跳出之后就是在最后一个结点 大于原有最大的数
        cur->next = t->next;
        preHead->next->pre = cur;
        cur->pre = t->pre;
        t->next  = cur;
    }
}
int deque(ListNode *preHead)
{
    if(!preHead->next->val)
        return NULL;

    ListNode *head = preHead->next;
    //deque 输出最小的 也就是循环队列的 队头
    int data = head->val;
    head->pre->next = head->next;
    head->next->pre = head->pre;
    preHead->next = head->next;
    delete head;
    return data;
}

void traverseList(ListNode *preHead)
{
    ListNode *t = preHead->next;
    cout<<t->val<<" ";
    t = t->next;

    while(t->next != preHead->next)
    {
        cout<<t->val<<" ";
        t = t->next;

    }
    cout<<endl;
}


int main()
{
    ListNode *preHead = createList();
    ListNode *head = preHead->next;

    //初始化完毕 输入数据插入到循环链表
    int nums;
    cin>>nums;
    for(int i=0; i<nums; i++)
    {
        int data;
        cin>>data;
        insertNodeList(preHead,data);
    }

    traverseList(preHead);

    //依次取出循环链表的最小值
    for(int i=0; i<nums-1; i++)
    {
        cout<<"==================="<<endl;
        deque(head);
        traverseList(head);
    }

    return 0;
}